/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ucb.ihm.view;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ResourceBundle;
import java.util.Scanner;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author gabriel
 */
@WebServlet(urlPatterns="/load/*")
public class LoadServlet extends HttpServlet {

    private static final ResourceBundle configuracoes = ResourceBundle.getBundle("configuracoes");
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uri = req.getRequestURI();
        String name = uri.substring(uri.lastIndexOf('/')+1);
        
        String page = configuracoes.getString(name+".page");
        String title = configuracoes.getString(name+".title"); 
        
        File file = new File(req.getServletContext().getRealPath(page));
        Scanner scn = new Scanner(new FileInputStream(file));
        
        resp.setCharacterEncoding("UTF-8");
        PrintWriter json = resp.getWriter();
        json.append("{\"title\":\"");
        json.append(title);
        json.append("\",\"page\":\"");
        
        while(scn.hasNext()){
            json.append(scn.nextLine().replace("\"", "\\\""));
        }
        
        json.append("\"}");
        
    }
    
}
